let express = require("express")
let router = express.Router()


const query = require("../tools/db")
const { api_url } = require("../config/config")

// 地区
router.get("/areas", async (req, res, next) => {

    let sql = `select area_name  from e_area`
    let [err, results] = await query(sql)

    if (err) {
        next("地区请求失败")
        return
    }

    /* let area_arr=[]
    result.forEach(ele => {
        area_arr.push(ele.area_name)   
    }); */


    let area_arr = results.map(ele => {
        return ele.area_name
    })


    res.send({
        status: 200,
        msg: "地区请求成功",
        results: area_arr
    })

})




// 年级
router.get("/grades", async (req, res, next) => {

    let sql = `select grade_name  from e_grade`
    let [err, results] = await query(sql)

    if (err) {
        next("年级请求失败")
        return
    }

    let grade_arr = results.map(ele => {
        return ele.grade_name
    })


    res.send({
        status: 200,
        msg: "年级请求成功",
        results: grade_arr
    })

})




// 学科
router.get("/subjects", async (req, res, next) => {

    let sql = `select subject_name  from e_subject`
    let [err, results] = await query(sql)

    if (err) {
        next("学科请求失败")
        return
    }

    let subject_arr = results.map(ele => {
        return ele.subject_name
    })


    res.send({
        status: 200,
        msg: "学科请求成功",
        results: subject_arr
    })

})



// 考试类别
router.get("/cates", async (req, res, next) => {

    let sql = `select category_name  from e_category`
    let [err, results] = await query(sql)

    if (err) {
        next("考试类别请求失败")
        return
    }

    let cate_arr = results.map(ele => {
        return ele.category_name
    })


    res.send({
        status: 200,
        msg: "考试类别请求成功",
        results: cate_arr
    })

})



// 列表页列表
router.get("/courselist", async (req, res, next) => {



    // 没有任何条件
    /* let sql=`select 
                    id,
                    cid,
                    title,
                    price,
                    concat("${api_url}","/uploads/","index/",image_src) as image_src,
                    area_name,
                    subject_name
             from e_course` */


             


    // 用户点击了筛选 : 地区  年级  学科  考试类别
    /* let { area_name, grade_name, subject_name, category_name } = req.query

    let strwhere = ``
    if (area_name) {
        strwhere += `and area_name="${area_name}" `
    }
    if (grade_name) {
        strwhere += `and grade_name="${grade_name}" `
    }
    if (subject_name) {
        strwhere += `and subject_name="${subject_name}" `
    }
    if (category_name) {
        strwhere += `and category_name="${category_name}" `
    }




    let sql = `select 
                    id,
                    cid,
                    title,
                    price,
                    concat("${api_url}","/uploads/","index/",image_src) as image_src,
                    area_name,
                    subject_name
            from e_course
            where  (status=1 ||status=2) 
            ${strwhere}
            ` */

    // 用户点击分页 需要传递页码
    let { area_name, grade_name, subject_name, category_name, page = 1, size = 6 } = req.query

    let strwhere = ``
    if (area_name) {
        strwhere += `and area_name="${area_name} "`
    }
    if (grade_name) {
        strwhere += `and grade_name="${grade_name}" `
    }
    if (subject_name) {
        strwhere += `and subject_name="${subject_name}" `
    }
    if (category_name) {
        strwhere += `and category_name="${category_name}" `
    }

    // index  下标，从哪个数据开始拿取
    // size 拿取几条，一页展示多少数据
    // page 当前页码
    let index = size * (page - 1)



    let sql = `select 
                        id,
                        cid,
                        title,
                        price,
                        concat("${api_url}","/uploads/","index/",image_src) as image_src,
                        area_name,
                        subject_name
                from e_course
                where  (status=1 ||status=2) 
                ${strwhere}
                limit ${index},${size}
                `
    let [err, results] = await query(sql)

    if(err){
        next("列表请求失败")
        return
    }



    // 总页码：Math.ceil(总数据/size )
    let sqls = `select 
                    id,
                    cid,
                    title,
                    price,
                    concat("${api_url}","/uploads/","index/",image_src) as image_src,
                    area_name,
                    subject_name
                from e_course
                where  (status=1 ||status=2) 
                ${strwhere}
                `
    let [errs, resultss] = await query(sqls)
    if(errs){
        next("列表总数量请求失败")
        return
    }

    let pages = Math.ceil(resultss.length/size)



    res.send({
        status: 200,
        msg: "列表请求成功",
        results:{
            data:results,
            pages,//总页码
            page:Number(page),//当前页
            rows:resultss.length//总数量
        }
    })

})






module.exports = router